import _regeneratorRuntime from "@babel/runtime/regenerator";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
var _excluded = ["tenantId"];

/**
 * service - 并发管理/并发请求
 * @date: 2018-9-10
 * @version: 1.0.0


 */
import request from 'utils/request';
import { CPAAS_SDR } from 'utils/config';
import { getCurrentOrganizationId, isTenantRoleLevel, parseParameters } from 'utils/utils';
var organizationId = getCurrentOrganizationId();
var organizationRoleLevel = isTenantRoleLevel();
/**
 * 请求API前缀
 * @type {string}
 */

var prefix = "".concat(CPAAS_SDR, "/v1");
/**
 * 数据查询
 * @async
 * @function fetchRequestList
 * @param {object} params - 查询条件
 * @param {?string} params.tenantId - 租户ID
 * @returns {object} fetch Promise
 */

export function fetchRequestList(_x) {
  return _fetchRequestList.apply(this, arguments);
}
/**
 * 创建
 * @async
 * @function createRequest
 * @param {String} params - 保存参数
 */

function _fetchRequestList() {
  _fetchRequestList = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(params) {
    var param, tenantId, others;
    return _regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            param = parseParameters(params);
            tenantId = param.tenantId, others = _objectWithoutProperties(param, _excluded);
            return _context.abrupt("return", request(organizationRoleLevel ? "".concat(prefix, "/").concat(organizationId, "/concurrent-requests") : "".concat(prefix, "/concurrent-requests"), {
              method: 'GET',
              query: others
            }));

          case 3:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  }));
  return _fetchRequestList.apply(this, arguments);
}

export function createRequest(_x2) {
  return _createRequest.apply(this, arguments);
}
/**
 * 查询详情
 * @async
 * @function fetchRequestDetail
 * @param {Object} params - 查询参数
 */

function _createRequest() {
  _createRequest = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(params) {
    return _regeneratorRuntime.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            return _context2.abrupt("return", request(organizationRoleLevel ? "".concat(prefix, "/").concat(organizationId, "/concurrent-requests") : "".concat(prefix, "/concurrent-requests"), {
              method: 'POST',
              body: params
            }));

          case 1:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2);
  }));
  return _createRequest.apply(this, arguments);
}

export function fetchRequestDetail(_x3) {
  return _fetchRequestDetail.apply(this, arguments);
}
/**
 * 查询请求参数
 * @async
 * @function fetchParams
 * @param {Object} params - 查询参数
 */

function _fetchRequestDetail() {
  _fetchRequestDetail = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(params) {
    return _regeneratorRuntime.wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            return _context3.abrupt("return", request(organizationRoleLevel ? "".concat(prefix, "/").concat(organizationId, "/concurrent-requests/").concat(params.requestId) : "".concat(prefix, "/concurrent-requests/").concat(params.requestId), {
              method: 'GET'
            }));

          case 1:
          case "end":
            return _context3.stop();
        }
      }
    }, _callee3);
  }));
  return _fetchRequestDetail.apply(this, arguments);
}

export function fetchParams(_x4) {
  return _fetchParams.apply(this, arguments);
}
/**
 * 删除JOB
 * @async
 * @function deleteJobInfo
 * @param {String} params.tenantId - 租户ID
 */

function _fetchParams() {
  _fetchParams = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4(params) {
    return _regeneratorRuntime.wrap(function _callee4$(_context4) {
      while (1) {
        switch (_context4.prev = _context4.next) {
          case 0:
            return _context4.abrupt("return", request(organizationRoleLevel ? "".concat(prefix, "/").concat(organizationId, "/concurrent-params") : "".concat(prefix, "/concurrent-params"), {
              method: 'GET',
              query: params
            }));

          case 1:
          case "end":
            return _context4.stop();
        }
      }
    }, _callee4);
  }));
  return _fetchParams.apply(this, arguments);
}

export function deleteJobInfo(_x5) {
  return _deleteJobInfo.apply(this, arguments);
}
/**
 * 暂停
 * @async
 * @function pauseJobInfo
 * @param {String} params.jobId - 任务ID
 */

function _deleteJobInfo() {
  _deleteJobInfo = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5(params) {
    return _regeneratorRuntime.wrap(function _callee5$(_context5) {
      while (1) {
        switch (_context5.prev = _context5.next) {
          case 0:
            return _context5.abrupt("return", request(organizationRoleLevel ? "".concat(prefix, "/").concat(organizationId, "/concurrent-requests") : "".concat(prefix, "/concurrent-requests"), {
              method: 'DELETE',
              body: params
            }));

          case 1:
          case "end":
            return _context5.stop();
        }
      }
    }, _callee5);
  }));
  return _deleteJobInfo.apply(this, arguments);
}

export function pauseJobInfo(_x6) {
  return _pauseJobInfo.apply(this, arguments);
}
/**
 * 恢复
 * @async
 * @function resumeJobInfo
 * @param {String} params.jobId - 任务ID
 */

function _pauseJobInfo() {
  _pauseJobInfo = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee6(params) {
    return _regeneratorRuntime.wrap(function _callee6$(_context6) {
      while (1) {
        switch (_context6.prev = _context6.next) {
          case 0:
            return _context6.abrupt("return", request(organizationRoleLevel ? "".concat(prefix, "/").concat(organizationId, "/job-info/pause") : "".concat(prefix, "/job-info/pause"), {
              method: 'POST',
              body: params
            }));

          case 1:
          case "end":
            return _context6.stop();
        }
      }
    }, _callee6);
  }));
  return _pauseJobInfo.apply(this, arguments);
}

export function resumeJobInfo(_x7) {
  return _resumeJobInfo.apply(this, arguments);
}
/**
 * 执行
 * @async
 * @function triggerJobInfo
 * @param {String} params.jobId - 任务ID
 */

function _resumeJobInfo() {
  _resumeJobInfo = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee7(params) {
    return _regeneratorRuntime.wrap(function _callee7$(_context7) {
      while (1) {
        switch (_context7.prev = _context7.next) {
          case 0:
            return _context7.abrupt("return", request(organizationRoleLevel ? "".concat(prefix, "/").concat(organizationId, "/job-info/resume") : "".concat(prefix, "/job-info/resume"), {
              method: 'POST',
              body: params
            }));

          case 1:
          case "end":
            return _context7.stop();
        }
      }
    }, _callee7);
  }));
  return _resumeJobInfo.apply(this, arguments);
}

export function triggerJobInfo(_x8) {
  return _triggerJobInfo.apply(this, arguments);
}
/**
 * 终止
 * @async
 * @function stopJob
 * @param {String} params.enabledFlag - 是否启用
 */

function _triggerJobInfo() {
  _triggerJobInfo = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee8(params) {
    return _regeneratorRuntime.wrap(function _callee8$(_context8) {
      while (1) {
        switch (_context8.prev = _context8.next) {
          case 0:
            return _context8.abrupt("return", request(organizationRoleLevel ? "".concat(prefix, "/").concat(organizationId, "/job-info/trigger") : "".concat(prefix, "/job-info/trigger"), {
              method: 'POST',
              body: params
            }));

          case 1:
          case "end":
            return _context8.stop();
        }
      }
    }, _callee8);
  }));
  return _triggerJobInfo.apply(this, arguments);
}

export function stopJob(_x9) {
  return _stopJob.apply(this, arguments);
}

function _stopJob() {
  _stopJob = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee9(params) {
    return _regeneratorRuntime.wrap(function _callee9$(_context9) {
      while (1) {
        switch (_context9.prev = _context9.next) {
          case 0:
            return _context9.abrupt("return", request(organizationRoleLevel ? "".concat(CPAAS_SDR, "/v1/").concat(organizationId, "/job-info/stop") : "".concat(CPAAS_SDR, "/v1/job-info/stop"), {
              method: 'POST',
              body: params
            }));

          case 1:
          case "end":
            return _context9.stop();
        }
      }
    }, _callee9);
  }));
  return _stopJob.apply(this, arguments);
}

export function fetchJobLog(_x10) {
  return _fetchJobLog.apply(this, arguments);
}
/**
 * 查询错误日志详情
 * @async
 * @function fetchJobLogError
 * @param {Object} params - 查询参数
 */

function _fetchJobLog() {
  _fetchJobLog = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee10(params) {
    var jobId, param;
    return _regeneratorRuntime.wrap(function _callee10$(_context10) {
      while (1) {
        switch (_context10.prev = _context10.next) {
          case 0:
            jobId = params.jobId;
            param = parseParameters(params);
            return _context10.abrupt("return", request(organizationRoleLevel ? "".concat(prefix, "/").concat(organizationId, "/job-logs/").concat(jobId, "/logs") : "".concat(prefix, "/job-logs/").concat(jobId, "/logs"), {
              method: 'GET',
              query: param
            }));

          case 3:
          case "end":
            return _context10.stop();
        }
      }
    }, _callee10);
  }));
  return _fetchJobLog.apply(this, arguments);
}

export function fetchJobLogError(_x11) {
  return _fetchJobLogError.apply(this, arguments);
}
/**
 * 删除
 * @async
 * @function deleteJobLog
 * @param {String} params.enabledFlag - 是否启用
 */

function _fetchJobLogError() {
  _fetchJobLogError = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee11(params) {
    return _regeneratorRuntime.wrap(function _callee11$(_context11) {
      while (1) {
        switch (_context11.prev = _context11.next) {
          case 0:
            return _context11.abrupt("return", request(organizationRoleLevel ? "".concat(CPAAS_SDR, "/v1/").concat(organizationId, "/job-logs/").concat(params.logId, "/error-detail") : "".concat(CPAAS_SDR, "/v1/job-logs/").concat(params.logId, "/error-detail"), {
              method: 'GET',
              responseType: 'text'
            }));

          case 1:
          case "end":
            return _context11.stop();
        }
      }
    }, _callee11);
  }));
  return _fetchJobLogError.apply(this, arguments);
}

export function deleteJobLog(_x12) {
  return _deleteJobLog.apply(this, arguments);
}
/**
 * 获取任务进度
 * @async
 * @function fetchProgress
 * @param {Object} params - 查询参数
 */

function _deleteJobLog() {
  _deleteJobLog = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee12(params) {
    return _regeneratorRuntime.wrap(function _callee12$(_context12) {
      while (1) {
        switch (_context12.prev = _context12.next) {
          case 0:
            return _context12.abrupt("return", request(organizationRoleLevel ? "".concat(CPAAS_SDR, "/v1/").concat(organizationId, "/job-logs") : "".concat(CPAAS_SDR, "/v1/job-logs"), {
              method: 'DELETE',
              body: params
            }));

          case 1:
          case "end":
            return _context12.stop();
        }
      }
    }, _callee12);
  }));
  return _deleteJobLog.apply(this, arguments);
}

export function fetchProgress(_x13) {
  return _fetchProgress.apply(this, arguments);
}

function _fetchProgress() {
  _fetchProgress = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee13(params) {
    return _regeneratorRuntime.wrap(function _callee13$(_context13) {
      while (1) {
        switch (_context13.prev = _context13.next) {
          case 0:
            return _context13.abrupt("return", request(organizationRoleLevel ? "".concat(CPAAS_SDR, "/v1/").concat(organizationId, "/job-logs/").concat(params.logId, "/progress") : "".concat(CPAAS_SDR, "/v1/job-logs/").concat(params.logId, "/progress"), {
              method: 'GET',
              responseType: 'text'
            }));

          case 1:
          case "end":
            return _context13.stop();
        }
      }
    }, _callee13);
  }));
  return _fetchProgress.apply(this, arguments);
}